﻿using System;
using UnityEngine;

namespace RenderHeads.Media.AVProVideo
{
	// Token: 0x02000017 RID: 23
	[AddComponentMenu("AVPro Video/Apply To Material", 300)]
	[HelpURL("http://renderheads.com/product/avpro-video/")]
	public class ApplyToMaterial : MonoBehaviour
	{
		// Token: 0x0600007D RID: 125 RVA: 0x00005890 File Offset: 0x00003A90
		private void Awake()
		{
			if (ApplyToMaterial._propStereo == 0 || ApplyToMaterial._propAlphaPack == 0)
			{
				ApplyToMaterial._propStereo = Shader.PropertyToID("Stereo");
				ApplyToMaterial._propAlphaPack = Shader.PropertyToID("AlphaPack");
				ApplyToMaterial._propApplyGamma = Shader.PropertyToID("_ApplyGamma");
			}
			if (ApplyToMaterial._propChromaTex == 0)
			{
				ApplyToMaterial._propChromaTex = Shader.PropertyToID("_ChromaTex");
			}
			if (ApplyToMaterial._propUseYpCbCr == 0)
			{
				ApplyToMaterial._propUseYpCbCr = Shader.PropertyToID("_UseYpCbCr");
			}
		}

		// Token: 0x0600007E RID: 126 RVA: 0x00005910 File Offset: 0x00003B10
		private void LateUpdate()
		{
			bool flag = false;
			if (this._media != null && this._media.TextureProducer != null)
			{
				int textureCount = this._media.TextureProducer.GetTextureCount();
				for (int i = 0; i < textureCount; i++)
				{
					Texture texture = this._media.TextureProducer.GetTexture(i);
					if (texture != null)
					{
						this.ApplyMapping(texture, this._media.TextureProducer.RequiresVerticalFlip(), i);
						flag = true;
					}
				}
			}
			if (!flag)
			{
				this.ApplyMapping(this._defaultTexture, false, 0);
			}
		}

		// Token: 0x0600007F RID: 127 RVA: 0x000059B0 File Offset: 0x00003BB0
		private void ApplyMapping(Texture texture, bool requiresYFlip, int plane = 0)
		{
			if (this._material != null)
			{
				if (plane == 0)
				{
					if (string.IsNullOrEmpty(this._texturePropertyName))
					{
						this._material.mainTexture = texture;
						if (texture != null)
						{
							if (requiresYFlip)
							{
								this._material.mainTextureScale = new Vector2(this._scale.x, -this._scale.y);
								this._material.mainTextureOffset = Vector2.up + this._offset;
							}
							else
							{
								this._material.mainTextureScale = this._scale;
								this._material.mainTextureOffset = this._offset;
							}
						}
					}
					else
					{
						this._material.SetTexture(this._texturePropertyName, texture);
						if (texture != null)
						{
							if (requiresYFlip)
							{
								this._material.SetTextureScale(this._texturePropertyName, new Vector2(this._scale.x, -this._scale.y));
								this._material.SetTextureOffset(this._texturePropertyName, Vector2.up + this._offset);
							}
							else
							{
								this._material.SetTextureScale(this._texturePropertyName, this._scale);
								this._material.SetTextureOffset(this._texturePropertyName, this._offset);
							}
						}
					}
				}
				else if (plane == 1)
				{
					if (this._material.HasProperty(ApplyToMaterial._propUseYpCbCr))
					{
						this._material.EnableKeyword("USE_YPCBCR");
					}
					if (this._material.HasProperty(ApplyToMaterial._propChromaTex))
					{
						this._material.SetTexture(ApplyToMaterial._propChromaTex, texture);
						if (texture != null)
						{
							if (requiresYFlip)
							{
								this._material.SetTextureScale("_ChromaTex", new Vector2(this._scale.x, -this._scale.y));
								this._material.SetTextureOffset("_ChromaTex", Vector2.up + this._offset);
							}
							else
							{
								this._material.SetTextureScale("_ChromaTex", this._scale);
								this._material.SetTextureOffset("_ChromaTex", this._offset);
							}
						}
					}
				}
				if (this._media != null)
				{
					if (this._material.HasProperty(ApplyToMaterial._propStereo))
					{
						Helper.SetupStereoMaterial(this._material, this._media.m_StereoPacking, this._media.m_DisplayDebugStereoColorTint);
					}
					if (this._material.HasProperty(ApplyToMaterial._propAlphaPack))
					{
						Helper.SetupAlphaPackedMaterial(this._material, this._media.m_AlphaPacking);
					}
					if (this._material.HasProperty(ApplyToMaterial._propApplyGamma) && this._media.Info != null)
					{
						Helper.SetupGammaMaterial(this._material, this._media.Info.PlayerSupportsLinearColorSpace());
					}
				}
			}
		}

		// Token: 0x06000080 RID: 128 RVA: 0x00005CB0 File Offset: 0x00003EB0
		private void OnEnable()
		{
			if (string.IsNullOrEmpty(this._texturePropertyName))
			{
				this._originalTexture = this._material.mainTexture;
				this._originalScale = this._material.mainTextureScale;
				this._originalOffset = this._material.mainTextureOffset;
			}
			else
			{
				this._originalTexture = this._material.GetTexture(this._texturePropertyName);
				this._originalScale = this._material.GetTextureScale(this._texturePropertyName);
				this._originalOffset = this._material.GetTextureOffset(this._texturePropertyName);
			}
			this.LateUpdate();
		}

		// Token: 0x06000081 RID: 129 RVA: 0x00005D50 File Offset: 0x00003F50
		private void OnDisable()
		{
			if (string.IsNullOrEmpty(this._texturePropertyName))
			{
				this._material.mainTexture = this._originalTexture;
				this._material.mainTextureScale = this._originalScale;
				this._material.mainTextureOffset = this._originalOffset;
			}
			else
			{
				this._material.SetTexture(this._texturePropertyName, this._originalTexture);
				this._material.SetTextureScale(this._texturePropertyName, this._originalScale);
				this._material.SetTextureOffset(this._texturePropertyName, this._originalOffset);
			}
		}

		// Token: 0x0400006D RID: 109
		public Vector2 _offset = Vector2.zero;

		// Token: 0x0400006E RID: 110
		public Vector2 _scale = Vector2.one;

		// Token: 0x0400006F RID: 111
		public Material _material;

		// Token: 0x04000070 RID: 112
		public string _texturePropertyName;

		// Token: 0x04000071 RID: 113
		public MediaPlayer _media;

		// Token: 0x04000072 RID: 114
		public Texture2D _defaultTexture;

		// Token: 0x04000073 RID: 115
		private Texture _originalTexture;

		// Token: 0x04000074 RID: 116
		private Vector2 _originalScale = Vector2.one;

		// Token: 0x04000075 RID: 117
		private Vector2 _originalOffset = Vector2.zero;

		// Token: 0x04000076 RID: 118
		private static int _propStereo;

		// Token: 0x04000077 RID: 119
		private static int _propAlphaPack;

		// Token: 0x04000078 RID: 120
		private static int _propApplyGamma;

		// Token: 0x04000079 RID: 121
		private const string PropChromaTexName = "_ChromaTex";

		// Token: 0x0400007A RID: 122
		private static int _propChromaTex;

		// Token: 0x0400007B RID: 123
		private const string PropUseYpCbCrName = "_UseYpCbCr";

		// Token: 0x0400007C RID: 124
		private static int _propUseYpCbCr;
	}
}
